package com.dh.project.controller;

import com.dh.project.model.LableInfo;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.model.ServiceProject;
import com.dh.project.other.ModelRes;
import com.dh.project.service.LableInfoService;
import com.dh.project.service.ServiceProjectService;
import com.dh.project.utils.PageUtil;
import com.dh.project.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/4/17.
 */
@Controller
public class ProductController {

    private ServiceProjectService serviceProjectService;

    private LableInfoService lableInfoService;

    @Autowired
    public ProductController(ServiceProjectService serviceProjectService,LableInfoService lableInfoService){
        this.serviceProjectService = serviceProjectService;
        this.lableInfoService = lableInfoService;
    }

    /**
     * 产品内容列表
     * @param serviceProject
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectServiceProjectList",produces = "text/html;charset=utf-8")
    public String selectServiceProjectList(ServiceProject serviceProject, int page, Model model){

        //查询产品内容
        Map map = new HashMap();
        map.put("service_class",serviceProject.getService_class());//分类
        map.put("service_brand",serviceProject.getService_brand());//品牌
        map.put("service_kind",serviceProject.getService_kind());//种类
        map.put("service_standard",serviceProject.getService_standard());//规格
        map.put("service_assembly",serviceProject.getService_assembly());//部件
        map.put("service_back",serviceProject.getService_back()); //型号

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<ServiceProject> serviceProjects = serviceProjectService.selectServiceProjectListByMap(map);


        Map lableMap = new HashMap();
        //分类
        if (serviceProject.getService_class() != null){
            lableMap.put("lable_class_one",serviceProject.getService_class());
        }
        //种类
//        if (serviceProject.getService_kind() != null){
//            lableMap.put("lable_class_two",serviceProject.getService_kind());
//        }
        //Lable部分内容
        List<LableInfo> lableInfos = lableInfoService.selectByMap(lableMap);

        model.addAttribute("lableInfos",lableInfos);

        //分页部分
        PageModel pageModel = serviceProjectService.selectServiceProjectListByCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHrefByFive(serviceProject,1));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("serviceProject",serviceProject);
        model.addAttribute("serviceProjects",serviceProjects);
        return "service_project_list";
    }


    /**
     * 维修价格列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectMaintainList",produces = "text/html;charset=utf-8")
    public String selectMaintainList(Model model){
        Map map = new HashMap();
        map.put("service_class",7);
        List<ServiceProject> serviceProjects = serviceProjectService.selectByMap(map);
        model.addAttribute("serviceProjects",serviceProjects);
        return "maintain_list";
    }

    /**
     * 添加维修费
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertMaintain",produces = "text/html;charset=utf-8")
    public Object insertMaintain(ServiceProject serviceProject){
        Map map = new HashMap();
        map.put("service_class",7);
        List<ServiceProject> serviceProjects = serviceProjectService.selectByMap(map);
        if (serviceProjects.size() > 10){
            return new ModelRes(ModelRes.Status.ERROR,"添加失败，维修费用总数不能超过10条!");
        }
        serviceProject.setService_class(7);
        serviceProject.setService_brand(0);
        serviceProject.setService_kind(0);
        serviceProject.setService_standard(0);
        serviceProject.setService_assembly(0);
        serviceProject.setService_price(BigDecimal.valueOf(0.00));
        serviceProject.setMaintenance_cost(BigDecimal.valueOf(0.00));
        serviceProject.setCreatetime(String.valueOf(new Date().getTime()));
        serviceProject.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = serviceProjectService.insert(serviceProject);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }


    /**
     * 修改维修费内容
     * @param serviceProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateMaintain",produces = "text/html;charset=utf-8")
    public Object updateMaintain(ServiceProject serviceProject){
        ServiceProject serviceProject1 = serviceProjectService.selectById(serviceProject.getService_id());

        //修改价格
        if (serviceProject.getService_all_price() != null){
            serviceProject1.setService_all_price(serviceProject.getService_all_price());
        }
        //修改名称
        if (!StringUtil.isBlank(serviceProject.getService_content())){
            serviceProject1.setService_content(serviceProject.getService_content());
        }
        //修改备注
        if (!StringUtil.isBlank(serviceProject.getService_back())){
            serviceProject1.setService_back(serviceProject.getService_back());
        }
        //修改时间
        serviceProject1.setModifytime(String.valueOf(new Date().getTime()));

        boolean isSuccess = serviceProjectService.updateById(serviceProject1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除维修费内容  OR  删除服务项目价格
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteProject",produces = "text/html;charset=utf-8")
    public Object deleteProject(int admin_id){
        boolean isSuccess = serviceProjectService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }

}
